package com.jsle.financial.action;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.WebUtils;

import com.jsle.financial.domain.Company;
import com.jsle.financial.domain.Company_Resources;
import com.jsle.financial.domain.User;
import com.jsle.financial.domain.UserGroup;
import com.jsle.financial.domain.User_UserGroups;
import com.jsle.financial.service.UsergroupService;
import com.jsle.financial.util.AJAXUtil;
import com.jsle.financial.util.Constants;
import com.jsle.financial.util.Tools;
import com.jsle.financial.vo.Page;
import com.jsle.financial.vo.Result;

/**
 * 用户组管理Controller
 * @author wisdying
 * @date 2012-10-26
 */
@Controller
@RequestMapping("/usergroup")
public class UserGroupController {
	private Log log = LogFactory.getLog(CompangyController.class);
	@Resource
	private UsergroupService usergroupService;
	
	
	/**
	 * 新增用户组
	 */
	@RequestMapping(value = "/addUserGroup.do",method=RequestMethod.POST)
	@ResponseBody
	public void addUserGroup(UserGroup userGroup,HttpServletRequest request,HttpServletResponse response){
		Result rs = new Result();
		if(userGroup.getId() != null && !userGroup.getId().equals("") ){
			rs = usergroupService.delUserUserGroups(userGroup);//删除与该用户组相关的关联
			rs = usergroupService.updateUserGroup(userGroup);
		}else{
			rs = usergroupService.addUserGroup(userGroup);
		}
		if(rs.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("true"));
		}else{
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("false"));
		}
	}
	/**
	 * 删除单个用户组
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/delUserGroup.do",method=RequestMethod.POST)
	@ResponseBody
	public void delUserGroup(UserGroup userGroup,HttpServletRequest request,HttpServletResponse response){
		
		Result rs = usergroupService.delUserGroup(userGroup);//删除用户组
		if(rs.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			rs = usergroupService.delUserUserGroups(userGroup);//删除与该用户组相关的关联
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("true"));
		}else{
			AJAXUtil.writerJson(response,Tools.getReturnBooleanJson("false"));
		}
	}
	
	/**
	 * 初始修改用户信息
	 * @param list
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/initUpdateUserGroupInfo.do",produces = "application/json")
	@ResponseBody
	public void initUpdateUserGroupInfo(UserGroup ug,HttpServletRequest request,HttpServletResponse response){
		AJAXUtil.setCharacterEncoding(request,response);
		Result result = usergroupService.selUserGroup(ug);
		String userGroupInfoStr = "";
		String json = "";
		if(result.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			List<UserGroup> ugs = new ArrayList<UserGroup>();
			if(ug!=null){
				ugs.add((UserGroup)result.getResultValue());
				userGroupInfoStr = ControllerHelper.getUserGroupsDataJsonByList(ugs);
				json = "{success:true,userGroupInfo:["+userGroupInfoStr+"]}";
			}else{
				json = "{success:false,msg:\"查询用户组基础信息异常,请联系管理员!\" }";
			}
			
		}else{
			json = "{success:false,msg:\"查询用户组基础信息异常,请联系管理员!\" }";
		}
		System.out.println("json123"+json);
		AJAXUtil.writerJson(response,json);
	}
	
	/**
	 * 分页查询资源
	 * @param list
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/selUsergroups.do",produces = "application/json")
	@ResponseBody
	public void pageUsergroups(HttpServletRequest request,HttpServletResponse response){
		AJAXUtil.setCharacterEncoding(request,response);
		int pageSize = Integer.valueOf(WebUtils.findParameterValue(request, "pageSize"));
		int pageNo = Integer.valueOf(WebUtils.findParameterValue(request, "pageNo"));
		String companyCode = WebUtils.findParameterValue(request, "companyCode");
		String groupName = WebUtils.findParameterValue(request, "groupName");
		
		pageNo = (pageNo==0?0:(pageNo*pageSize+1));
		Page page = new Page(pageNo,pageSize);
		Result result = new Result();
		if(groupName!=null && !groupName.equals("")){
			result = usergroupService.queryUserGroupsByPage(page, "groupName", groupName);
		}else{
			result = usergroupService.queryUserGroupsByPage(page, "companyCode", companyCode);
		}
		
		String sb = "";
		Page pageResult = new Page();
		if(result.getResultCode()==Constants.FC_RESULT_SUCCESS_CODE){
			pageResult = (Page)result.getResultValue();
			List<UserGroup> ugs = new ArrayList<UserGroup>();
			if(pageResult.getData()!=null){
				ugs = pageResult.getData();
			}
			sb = ControllerHelper.getUserGroupsDataJsonByList(ugs);
		}
		
		String json = "{success:true,pageNo:" + pageNo + ",totalPageCount:"
		+ pageResult.getPageCount() + ",totalcount:"+pageResult.getTotalCount()+", data:[" + sb.toString()
		+ "]}";
		System.out.println("json#####"+json);
		AJAXUtil.writerJson(response,json);
	}
}
